<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\model\room;

use app\common\model\BaseModel;
use app\common\model\Building;
use app\common\model\Campus;

/**
 * 公寓房间记录模型
 * Class RoomApartmentLog
 * @package app\common\model\room
 */
class RoomApartmentLog extends BaseModel {

    protected $name = 'room_apartment_log';

    public function campus() {
        return $this->hasOne(Campus::class, 'id', 'cid');
    }

    public function building() {
        return $this->hasOne(Building::class, 'id', 'bid');
    }

    public function allDevice() {
        return $this->hasMany(RoomApartmentDeviceLog::class, 'rid', 'rid');
    }

    public function sDeviceStat() {
        return $this->hasMany(RoomApartmentDeviceLog::class, 'rid', 'rid')->where("device_type", 1);
    }

    public function dDeviceStat() {
        return $this->hasMany(RoomApartmentDeviceLog::class, 'rid', 'rid')->where("device_type", 2);
    }

    public static function getTypeList($val = null) {
        $arr = [
            "0" => "空房",
            "1" => "在职",
            "2" => "离退休",
            "3" => "家属",
        ];
        if (is_numeric($val)) {
            return $arr[$val] ?? '';
        }
        return $arr;
    }

    public static function getLiftList($val = null) {
        $arr = [
            '0' => '免费',
            '50' => '分摊50%',
            '100' => '分摊100%',
        ];
        if (is_numeric($val)) {
            return $arr[$val] ?? '';
        }
        return $arr;
    }

    public function getSortAttr($value, $item) {
        return $this->handle_sort($item['sort']);
    }

    public function getTypeTextAttr($value, $data) {
        return self::getTypeList($data['type']);
    }

    public function getLiftTypeTextAttr($value, $data) {
        if ($data['lift_type'] == '100') {
            $return_text = "平摊";
        } elseif ($data['lift_type'] == '0') {
            $return_text = "免费";
        } else {
            $return_text = $data['lift_type'] . "%";
        }
        return "分摊类型：" . $return_text;
    }

    public function getRoomAttr($value, $item) {
        return [
            "water_consumption" => $item['water_consumption'] ?? 0,
            "water_price" => $item['water_price'] ?? 0,
            "water_fee" => $item['water_fee'] ?? 0,
            "lift_fee" => $item['room_lift_fee'],
            "second_pressure_fee" => $item['room_second_pressure_fee'],
            "name" => $item['name'],
            "user_count" => $item['user_count'],
        ];
    }

}
